package com.pz.dx.dxm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pz.dx.common.exception.BizCodeEnum;
import com.pz.dx.common.exception.RRException;
import com.pz.dx.dxm.controller.vo.opinion.HandleOpinionReq;
import com.pz.dx.dxm.controller.vo.opinion.OpinionAddReq;
import com.pz.dx.dxm.controller.vo.opinion.OpinionListRes;
import com.pz.dx.dxm.dao.OpinionDao;
import com.pz.dx.dxm.entity.OpinionEntity;
import com.pz.dx.dxm.entity.UserEntity;
import com.pz.dx.dxm.service.OpinionService;
import com.pz.dx.dxm.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("opinionService")
public class OpinionServiceImpl extends ServiceImpl<OpinionDao, OpinionEntity> implements OpinionService {
    @Autowired
    private UserService userService;

    @Override
    public List<OpinionListRes> listOpinion(int model, Long opinionId, Long userId) {
        List<OpinionListRes> opinionResList = new ArrayList<>();
        QueryWrapper<OpinionEntity> queryWrapper = new QueryWrapper<>();
        switch (model){
            case 1:
                queryWrapper.eq("status", 1).or().eq("status", 2);
                break;
            case 2:
                queryWrapper.eq("user_id", userId);
                break;
            case 3:
                queryWrapper.eq("status", 0);
                break;
            case 4:
                if (opinionId == null) {
                    throw new RRException("opinionId is null");
                }
                queryWrapper.eq("opinion_id", opinionId);
                break;
            default:
                throw new RRException(BizCodeEnum.OPINION_LIST_MODEL_ILLEGAL);
        }
        List<OpinionEntity> list = this.list(queryWrapper);
        // 过滤出所有的一级opinion
        List<OpinionEntity> l1 = new ArrayList<>();
        List<OpinionEntity> l2 = new ArrayList<>();
        list.forEach(item -> {
            if (item.getFatherId() == null) {
                l1.add(item);
            } else {
                l2.add(item);
            }
        });

        l1.forEach(item1 -> {
            OpinionListRes opinionRes = new OpinionListRes();

            // 一级
            OpinionListRes.Opinion mainOpinion = new OpinionListRes.Opinion();
            BeanUtils.copyProperties(item1, mainOpinion);
            UserEntity user = userService.getById(item1.getUserId());
            mainOpinion.setUser(user);

            List<OpinionListRes.Opinion> childOpinion = new ArrayList<>();

            // 将二级opinion嵌入到一级opinion
            for (int i = 0; i < l2.size(); i++) {
                OpinionEntity item2 = l2.get(i);
                if(item2.getFatherId().equals(item1.getOpinionId())){
                    OpinionListRes.Opinion child = new OpinionListRes.Opinion();
                    BeanUtils.copyProperties(item2, child);
                    UserEntity userChild = userService.getById(item2.getUserId());
                    child.setUser(userChild);
                    childOpinion.add(child);
                    l2.remove(i);
                    i--;
                }
            }
            opinionRes.setMainOpinion(mainOpinion);
            opinionRes.setChildOpinion(childOpinion);
            opinionResList.add(opinionRes);
        });
        return opinionResList;
    }

    @Override
    public void addOpinion(Long userId, OpinionAddReq req) {
        OpinionEntity opinionEntity = new OpinionEntity(userId,
                req.getFatherId(),
                req.getTittle(),
                req.getContent()
                );
        save(opinionEntity);
    }

    @Override
    public OpinionEntity getOneOpinion(String opinionId) {
        return getById(opinionId);
    }

    @Override
    public void handleOpinion(HandleOpinionReq req) {
        switch (req.getHandleType()){
            case 0:
                //删除
                this.baseMapper.deleteById(req.getOpinionId());
                break;
            case 1:
                // 审核通过，修改status为1
                UpdateWrapper<OpinionEntity> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("opinion_id", req.getOpinionId());
                updateWrapper.set("status", 1);
                update(updateWrapper);
                break;
            default:
                throw new RRException("handleType illegal");
        }
    }
}
